package guda.stark.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.stark.biz.VillageBiz;
import guda.stark.dal.dao.VillageDAO;
import guda.stark.dal.dataobject.VillageDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class VillageBizImpl implements VillageBiz{

    private final static Logger logger = LoggerFactory.getLogger(VillageBizImpl.class);

    @Autowired
    private VillageDAO villageDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            VillageDO villageDO = villageDAO.getVillageById(id);
            bizResult.data.put("villageDO", villageDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Village error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = villageDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<VillageDO> villageList = villageDAO.selectForPage(baseQuery);
            bizResult.data.put("villageList", villageList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Village list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            villageDAO.delVillageById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete village error", e);
        }
        return bizResult;
    }

    public BizResult create(VillageDO villageDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = villageDAO.insert(villageDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Village error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(VillageDO villageDO) {
        BizResult bizResult = new BizResult();
        try {
            villageDAO.updateByIdSelective(villageDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Village error", e);
        }
        return bizResult;
    }


}
